package com.jindu.producer.system.controller;

import com.jindu.kernel.page.AlphaPage;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.utils.AlphaBeanUtils;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.producer.system.jpa.UserLogJpa;
import com.jindu.producer.system.model.SUserLogEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author jarvis
 * @date 2018/1/28
 */
@Api(description = "用户日志信息管理", tags = "UserLogController", basePath = "/userLog")
@RestController
@RequestMapping(value = "/userLog",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class UserLogController {

    @Autowired
    UserLogJpa userLogJpa;

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "根据条件，查询登录日志的数据列表", notes = "返回所有登录日志列表集合,用于展示")
    @RequestMapping(value = "getAll",method = RequestMethod.GET)
    public JsonResult<AlphaPage<SUserLogEntity>> getAll(@RequestParam(value = "account",required = false) String account,
                                                        @RequestParam(value = "type",required = false) Integer type,
                                                        @RequestParam(value = "successFlag",required = false) Integer successFlag,
                                                        @RequestParam(value = "loginPlatform",required = false) Integer loginPlatform,
                                                        @RequestParam(value = "beginTime",required = false) String beginTime,
                                                        @RequestParam(value = "endTime",required = false) String endTime,
                                                        @RequestParam(value = "pageNo",defaultValue = "0") Integer pageNo,
                                                        @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize){

        Specification querySpecifi = (Specification<SUserLogEntity>) (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();
            if(account != null){
                predicates.add(criteriaBuilder.like(root.get("account"),"%"+account+"%"));
            }
            if(type != null){
                predicates.add(criteriaBuilder.equal(root.get("type"),type));
            }
            if(successFlag != null){
                predicates.add(criteriaBuilder.equal(root.get("successFlag"),successFlag));
            }
            if(loginPlatform != null){
                predicates.add(criteriaBuilder.equal(root.get("loginPlatform"),loginPlatform));
            }
            if(beginTime != null){
                predicates.add(criteriaBuilder.greaterThan(root.<Date>get("logTime"), DateUtils.formatDateStr(beginTime)));
            }
            if(endTime != null){
                predicates.add(criteriaBuilder.lessThan(root.<Date>get("logTime"), DateUtils.formatDateStr(endTime)));
            }

            return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
        };
        Sort sort = new Sort(Sort.Direction.DESC, "logTime");
        Page<SUserLogEntity> entities=userLogJpa.findAll(querySpecifi,new PageRequest(pageNo, pageSize,sort));
        JsonResult jsonResult = new AlphaPage<>(entities).toJsonResult();
        return jsonResult;
    }

    @ApiImplicitParams({
            @ApiImplicitParam(name = "Authorization", value = "token" , required = false, dataType = "String", paramType = "header")
    })
    @ApiOperation(value = "更新和保存登录日志的数据",notes = "根据标签的UUID,更新和保存登录日志数据")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public JsonResult<SUserLogEntity> save(@RequestBody SUserLogEntity userLogEntity){

        SUserLogEntity sUserLogEntity=userLogJpa.findById(userLogEntity.getId());
        if(sUserLogEntity==null){
            userLogEntity.setId(IdGenerator.uuid());
            userLogEntity.setLogTime(new Timestamp(DateUtils.getCurrentTime()));
            return JsonResult.getSuccessResult(userLogJpa.saveAndFlush(userLogEntity));
        }else {
            BeanUtils.copyProperties(userLogEntity,sUserLogEntity, AlphaBeanUtils.getNullPropertyNames(userLogEntity));
            return JsonResult.getSuccessResult(userLogJpa.saveAndFlush(sUserLogEntity));
        }
    }




}
